/*
https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/zui-da-lian-xu-1de-ge-shu-iii-by-leetcod-hw12/
 */
public class Solution1004 {
    public int longestOnes(int[] nums, int k) {
        int l=0;
        int cur=0;
        int ans=0;
        for (int r=0;r<nums.length;r++){
            if (nums[r]==0){
                cur++;
                if (cur>k) {
                    while (l < r && nums[l] == 1) {
                        l++;
                    }
                    l++;
                    cur--;
                }
            }
            ans=Math.max(ans,r-l+1);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1004().longestOnes(new int[]{0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1},3));
    }
}
